package com.property.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.property.pojo.User;
import com.property.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    //接收传过来的用户名和密码 登录
    @RequestMapping("login")
    public String login(@RequestParam("userName") String username, String password,HttpSession session){
            String message = "登录成功";
            User user = userService.findUserByName(username);
           if(username ==null || password ==null || username.equals("")|| password.equals("")){
               message="用户名密码不能为空";
               //判断用户名是否存在
           }else if(user == null){
                message="用户不存在";
                //判断密码是否正确
           }else if(!user.getuPassword().equals(password)){
               message="密码不正确";
           }else{
               session.setAttribute("user",user);
           }
        return JSON.toJSONString(message);
    }

    //获取session中的数据
    @RequestMapping("getUser")
    public String getSession(HttpSession session){
        User user = (User) session.getAttribute("user");
        return JSON.toJSONString(user);
    }

    //退出登录
    @RequestMapping("loginOut")
    public String loginOut(HttpSession session){
        session.removeAttribute("user");
        return JSON.toJSONString("true");
    }

    //查询所有的用户
    @RequestMapping("findAllUser")
    public String findAllUser(Integer page){
        return userService.findAllUser(page);
    }
    //停用启用
    @RequestMapping("stopUser")
    public String stopUser(Integer uId){
        int num = userService.stopUser(uId);
        return JSON.toJSONString(num >0 ? "成功":"失败");
    }
    @RequestMapping("startUser")
    public String startUser(Integer uId){
        int num = userService.startUser(uId);
        return JSON.toJSONString(num >0 ? "成功":"失败");
    }
    //新增用户
    @RequestMapping("addUser")
    public String addUser(User user,String password2){
        return userService.updateOrSaveUser(user,password2);
    }
    //删除用户
    @RequestMapping("deleteUserByuId")
    public String deleteUser(Integer uId){
        int num = userService.deleteUserByuId(uId);
        return JSON.toJSONString(num >0 ? "成功":"失败");
    }
    //通过id查询用户 回显数据
    @RequestMapping("findByuId")
    public String findByuId(Integer uId){
        return JSON.toJSONString(userService.findByuId(uId));
    }



}
